﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeleteLog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<string> files;

        private async void WatingTask<T>(Func<Task<T>> task, Action<T> action)
        {
            this.label1.Visible = true;
            action(await task());
            this.label1.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WatingTask(SearchPatternFiles, p => { this.richTextBox1.Text = p; });
        }
        private async Task<string> SearchPatternFiles()
        {
            await Task.Delay(5000);
            return await Task.Run(() =>
            {
                //aic20140102_11_04.log
                var path = Environment.CurrentDirectory;
                files = Directory.EnumerateFiles(path, "aic*", SearchOption.TopDirectoryOnly).ToList();
                files.AddRange(Directory.EnumerateFiles(path, "log*", SearchOption.TopDirectoryOnly));
                files.AddRange(Directory.EnumerateFiles(path, "smf*", SearchOption.TopDirectoryOnly));
                StringBuilder sb = new StringBuilder();
                files.ForEach(p =>
                {
                    sb.AppendLine(p);
                });
                return sb.ToString();
            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (files == null)
                return;
            for (int i = 0; i < files.Count; i++)
            {
                try
                {
                    File.Delete(files[i]);
                }
                catch (Exception)
                {
                }
                this.Text = "                   " + i + "/" + files.Count;
            }
        }
        //hostroy
        List<string> historyDries = new List<string>();

        private void button3_Click(object sender, EventArgs e)
        {
            var path = Environment.CurrentDirectory;
            var dires = Directory.EnumerateDirectories(path, "2013*", SearchOption.TopDirectoryOnly);
            this.richTextBox1.Clear();
            foreach (var item in dires)
            {
                historyDries.Add(item);
                this.richTextBox1.AppendText(item);
                this.richTextBox1.AppendText(Environment.NewLine);
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            foreach (var item in historyDries)
            {
                Directory.Delete(item, true);
            }
        }

        List<string> historyFiles = new List<string>();
        private void button4_Click(object sender, EventArgs e)
        {
            WatingTask(SearchPatternDateFiles, p => { this.richTextBox1.Text = p; });
        }

        private async Task<string> SearchPatternDateFiles()
        {
            return await Task.Run(() =>
            {
                var path = Environment.CurrentDirectory;
                var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories);

                foreach (var item in files)
                {
                    var lasttime = File.GetLastWriteTime(item);
                    if (lasttime.AddDays(45) < DateTime.Now)
                    {
                        historyFiles.Add(item);
                    }
                }

                DirectoryInfo robotDire = new DirectoryInfo(path);
                var robotPath = Path.Combine(robotDire.Parent.FullName, "Robot", "Robotlog");
                files = Directory.EnumerateFiles(robotPath, "*", SearchOption.AllDirectories);

                foreach (var item in files)
                {
                    var lasttime = File.GetLastWriteTime(item);
                    if (lasttime.AddDays(15) < DateTime.Now)
                    {
                        historyFiles.Add(item);
                    }
                }
                StringBuilder sb = new StringBuilder();
                historyFiles.ForEach(p =>
                {
                    sb.AppendLine(p);
                });
                return sb.ToString();
            });
        }
        private void button6_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < historyFiles.Count; i++)
            {
                try
                {
                    File.Delete(historyFiles[i]);
                }
                catch (Exception)
                {
                }
                this.Text = "                   " + i + "/" + historyFiles.Count;
            }
        }


    }
}
